﻿using System;
using System.Collections.Generic;
using System.Windows.Forms;
using ESRI.ArcGIS.ArcMapUI;
using ESRI.ArcGIS.Carto;

namespace ODM_Code.Forms
{
    public partial class SetDecisionVariablesForm : Form
    {
        private IMxDocument doc;
        private Code.SetDecisionVariablesCode svc;
        private string dbfPath;
        private string inputPath;
        private string alternativePath;
        private Dictionary<int, string> layerList;

        public SetDecisionVariablesForm()
        {
            InitializeComponent();
            doc = null;
        }

        public SetDecisionVariablesForm(IMxDocument doc)
        {
            InitializeComponent();
            this.doc = doc;
            svc = new Code.SetDecisionVariablesCode(doc);
            dbfPath = "C:\\magfire\\" + ConfigSettings.ReadSetting("analysisArea");
            inputPath = dbfPath + "\\input";
            layerList = new Dictionary<int, string>();
        }

        private void butAppend_Click(object sender, EventArgs e)
        {
            if (this.CheckInputs() == false)
            {
                MessageBox.Show("You must fill in all fields.", "Input Error", MessageBoxButtons.OK, MessageBoxIcon.Error);
                return;
            }

            ILayer treatLayer = doc.FocusMap.get_Layer(cmbTreatmentLayer.SelectedIndex);
            IFeatureLayer fcl = (IFeatureLayer)treatLayer;

            //switch (cmbInputMethod.Text)
            //{
            //    case "All Features":
            //        svc.LookupFeatures(fcl, false);
            //        break;
            //    case "Selected Features":
            //        svc.LookupFeatures(fcl, true);
            //        break;
            //}

            string action = cmbAction.Text;
            int period = int.Parse(cmbPeriod.Text);

            List<int> selectedFeatures = svc.LookupFeatures(fcl, true);
            List<int> projectIDs = svc.IntPrjIdLookup(inputPath, selectedFeatures, action, period);

            string pathToDVFile = alternativePath + "\\decisionvariables.txt";
            svc.WriteDVFile(pathToDVFile, projectIDs);

            this.Close();
        }

        private void SetDecisionVariablesForm_Load(object sender, EventArgs e)
        {
            // Populate Alternative box
            string[] files = System.IO.Directory.GetDirectories(inputPath);
            foreach (string s in files)
            {
                string[] spl = s.Split('\\');
                int i = spl.Length;

                string addThis = spl[i - 1];
                if (addThis.StartsWith("S"))
                {
                    this.cmbAlternative.Items.Add(addThis);
                }
            }

            // Populate Layer box
            for (int i = 0; i < doc.FocusMap.LayerCount; i++)
            {
                cmbTreatmentLayer.Items.Add(doc.FocusMap.get_Layer(i).Name);
                layerList[i] = doc.FocusMap.get_Layer(i).Name;
            }

            // Populate available treatments
            List<string> treatments = svc.GetTreatments(dbfPath);
            foreach (string s in treatments)
            {
                cmbAction.Items.Add(s);
            }

            // Populate periods
            int[] periods = svc.GetPeriods(dbfPath);
            foreach (int i in periods)
            {
                cmbPeriod.Items.Add(i);
            }
        }

        private void cmbAlternative_SelectedIndexChanged(object sender, EventArgs e)
        {
            alternativePath = inputPath + "\\" + cmbAlternative.Text;

            //tbTreatmentPath.Text = svc.GetTreatFilePath(alternativePath);
            //tbTreatmentPath.Enabled = false;
        }

        private bool CheckInputs()
        {
            bool good = true;
            ComboBox[] boxes = { cmbAlternative, cmbTreatmentLayer, cmbInputMethod, cmbPeriod, cmbPeriod };
            foreach (ComboBox cb in boxes)
            {
                if (cb.Text.Equals(""))
                {
                    good = false;
                }
            }

            return good;
        }

    }
}
